#include <iostream>

using namespace std;

bool isVaildInputs = false;
// 方法一：遍历(O(n2)
// 方法二：
int findGreatestSumOfSubArray(int * data, int length){
    if ( data == nullptr || length <= 0 ){
        return isVaildInputs;
    }

    int maxSum = 0;
    int sum = 0;
    for ( int i = 0; i < length; i++ ){
        sum += data[i];
        if ( sum < 0 ){
            // 舍弃和为负数的数据
            sum = 0;
        }else if ( sum > maxSum ){
            maxSum = sum;
        }
    }
    return maxSum;

}

int main(void){
    int data[] = {1,-2,3,10,-4,7,2,-5};
    // 
    cout << "result : " << findGreatestSumOfSubArray(data, 8) << endl;
    // 空指针测试
    cout << "result : " << findGreatestSumOfSubArray(nullptr, 0) << endl;
    return 0;
}